package com.prs.crm.action.crm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;

import com.prs.crm.domain.crm.CodeNumber;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.crm.CodeNumberService;

public class CodeNumberAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private CodeNumber codeNumber;
	private CodeNumberService codeNumberService;
	private String state;
	@Action("listCode")
	public String listCode(){
		//更新编码
		Calendar a=Calendar.getInstance();
		String year=String.valueOf(a.get(Calendar.YEAR));
		String month="";
		String date="";
		if(a.get(Calendar.MONTH)+1<10){
			month="0"+(a.get(Calendar.MONTH)+1);
		}else{
			month=String.valueOf(a.get(Calendar.MONTH)+1);
		}
		if(a.get(Calendar.DATE)<10){
			date="0"+a.get(Calendar.DATE);
		}else{
			date=String.valueOf(a.get(Calendar.DATE));
		}
		List<CodeNumber> list=this.codeNumberService.getAllCodeNumber();
		for(int c=0;c<list.size();c++){
			CodeNumber code=list.get(c);
			List<String> edit=new ArrayList<String>();
			String[] ee=code.getEdit().split(",");
			String[] ss=code.getSample().split(",");
			for(int i=0;i<ee.length;i++){
				if(ee[i].equals("MM")){
					ss[i]=month;
				}else if(ee[i].equals("DD")){
					ss[i]=date;
				}else if(ee[i].equals("YYYY")){
					ss[i]=year;
				}
//				else if(ee[i].equals("##")){
//					String nu="";
//					for(int d=0;d<Integer.parseInt(code.getCodeNum())-1;d++){
//						nu=nu+"0";
//					}
//					nu=nu+"1";
//					ss[i]=nu;
//				}
				if(i==ee.length-1){
					edit.add(ss[i]);
				}else{
					edit.add(ss[i]+",");
				}
			}
			String kk="";
			for(int i=0;i<edit.size();i++){
				kk=kk+edit.get(i);
			}
			String[] k=kk.split(",");
			String kk2="";
			for(int i=0;i<k.length;i++){
				kk2=kk2+k[i];
			}
			code.setSample(kk);
			code.setCodeSample(kk2);
			this.codeNumberService.submit(code);
		}
		return SUCCESS;
	}
	@Action("editCustomerCode1")
	public String editCustomerCode1(){
		this.codeNumber=this.codeNumberService.getCodeByState("客户");
		return SUCCESS;
	}
	@Action("editCustomerCode2")
	public String editCustomerCode2(){
		this.codeNumber=this.codeNumberService.getCodeByState("代理商");
		return SUCCESS;
	}
	@Action("editCustomerCode3")
	public String editCustomerCode3(){
		this.codeNumber=this.codeNumberService.getCodeByState("项目");
		return SUCCESS;
	}
	@Action("editCustomerCode4")
	public String editCustomerCode4(){
		this.codeNumber=this.codeNumberService.getCodeByState("合同");
		return SUCCESS;
	}
	@Action("editCustomerCode5")
	public String editCustomerCode5(){
		this.codeNumber=this.codeNumberService.getCodeByState("供应商");
		return SUCCESS;
	}
	
	@Action(value = "saveCodeNumber1", results = {
			@Result(name = "success", type = "redirect", location = "listCode"),
			@Result(name = "error", type = "chain", location = "editCustomerCode") })
	public String saveCodeNumber1() {
		CodeNumber code=this.getCodeNumberService().getCodeByState("客户");
		if (code==null) {
			code=new CodeNumber();
			code.setState("客户");
			code.setAddMode(codeNumber.getAddMode());
			code.setCodeEdit(codeNumber.getCodeEdit());
			code.setCodeNum(codeNumber.getCodeNum());
			code.setCodeSample(codeNumber.getCodeSample());
			code.setEdit(codeNumber.getEdit());
			code.setNowCode(codeNumber.getNowCode());
			code.setSample(codeNumber.getSample());
			this.getCodeNumberService().save(code);
		}else {
			code.setAddMode(codeNumber.getAddMode());
			code.setCodeEdit(codeNumber.getCodeEdit());
			code.setCodeNum(codeNumber.getCodeNum());
			code.setCodeSample(codeNumber.getCodeSample());
			code.setEdit(codeNumber.getEdit());
			code.setNowCode(codeNumber.getNowCode());
			code.setSample(codeNumber.getSample());
			this.codeNumberService.submit(code);
		}
		codeNumber=code;
		return SUCCESS;
	}
	@Action(value = "saveCodeNumber2", results = {
			@Result(name = "success", type = "redirect", location = "listCode"),
			@Result(name = "error", type = "chain", location = "editCustomerCode") })
	public String saveCodeNumber2(){
		CodeNumber code1=this.getCodeNumberService().getCodeByState("代理商");
		if(code1==null){
			code1=new CodeNumber();
			code1.setState("代理商");
			code1.setAddMode(codeNumber.getAddMode());
			code1.setCodeEdit(codeNumber.getCodeEdit());
			code1.setCodeNum(codeNumber.getCodeNum());
			code1.setCodeSample(codeNumber.getCodeSample());
			code1.setEdit(codeNumber.getEdit());
			code1.setNowCode(codeNumber.getNowCode());
			code1.setSample(codeNumber.getSample());
			this.getCodeNumberService().save(code1);
		}else{
			code1.setAddMode(codeNumber.getAddMode());
			code1.setCodeEdit(codeNumber.getCodeEdit());
			code1.setCodeNum(codeNumber.getCodeNum());
			code1.setCodeSample(codeNumber.getCodeSample());
			code1.setEdit(codeNumber.getEdit());
			code1.setNowCode(codeNumber.getNowCode());
			code1.setSample(codeNumber.getSample());
			this.codeNumberService.submit(code1);
		}
		codeNumber=code1;
		return SUCCESS;
	}
	@Action(value = "saveCodeNumber3", results = {
			@Result(name = "success", type = "redirect", location = "listCode"),
			@Result(name = "error", type = "chain", location = "editCustomerCode") })
	public String saveCodeNumber3(){
		CodeNumber code2=this.getCodeNumberService().getCodeByState("项目");
		if(code2==null){
			code2=new CodeNumber();
			code2.setState("项目");
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.getCodeNumberService().save(code2);
		}else{
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.codeNumberService.submit(code2);
		}
		codeNumber=code2;
		return SUCCESS;
	}
	@Action(value = "saveCodeNumber4", results = {
			@Result(name = "success", type = "redirect", location = "listCode"),
			@Result(name = "error", type = "chain", location = "editCustomerCode") })
	public String saveCodeNumber4(){
		CodeNumber code2=this.getCodeNumberService().getCodeByState("合同");
		if(code2==null){
			code2=new CodeNumber();
			code2.setState("合同");
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.getCodeNumberService().save(code2);
		}else{
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.codeNumberService.submit(code2);
		}
		codeNumber=code2;
		return SUCCESS;
	}
	@Action(value = "saveCodeNumber5", results = {
			@Result(name = "success", type = "redirect", location = "listCode"),
			@Result(name = "error", type = "chain", location = "editCustomerCode") })
	public String saveCodeNumber5(){
		CodeNumber code2=this.getCodeNumberService().getCodeByState("供应商");
		if(code2==null){
			code2=new CodeNumber();
			code2.setState("供应商");
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.getCodeNumberService().save(code2);
		}else{
			code2.setAddMode(codeNumber.getAddMode());
			code2.setCodeEdit(codeNumber.getCodeEdit());
			code2.setCodeNum(codeNumber.getCodeNum());
			code2.setCodeSample(codeNumber.getCodeSample());
			code2.setEdit(codeNumber.getEdit());
			code2.setNowCode(codeNumber.getNowCode());
			code2.setSample(codeNumber.getSample());
			this.codeNumberService.submit(code2);
		}
		codeNumber=code2;
		return SUCCESS;
	}
	public CodeNumber getCodeNumber() {
		return codeNumber;
	}
	public void setCodeNumber(CodeNumber codeNumber) {
		this.codeNumber = codeNumber;
	}
	public CodeNumberService getCodeNumberService() {
		return codeNumberService;
	}
	public void setCodeNumberService(CodeNumberService codeNumberService) {
		this.codeNumberService = codeNumberService;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}

}
